namespace CommonTagTime
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Web;

    public class clsConnection
    {
        public clsConnection()
        {
            this._Init();
        }

        public clsConnection(enmDBType DBType)
        {
            this._Init();
            this.ConnectingString = ConfigurationManager.AppSettings["ConnectingString"];
            this.DBType = DBType;
            this.Connect();
        }

        public clsConnection(byte DBType, string ConnectingString)
        {
            this._Init();
            this.ConnectingString = ConnectingString;
            this.DBType = (enmDBType) DBType;
            this.Connect();
        }

        public clsConnection(string DatabaseName, string FolderPath)
        {
            this.DBType = enmDBType.MS_ACCESS;
            this.ConnectingString = "Provider=Microsoft.Jet.oleDb.4.0;data source=" + HttpContext.Current.Server.MapPath(FolderPath + "/" + DatabaseName);
            this.Connect();
        }

        public clsConnection(string FileName, string FolderPath, enmFileReadHeader ReadHeader)
        {
            this.DBType = enmDBType.MS_EXCEL;
            if (ReadHeader == enmFileReadHeader.Allow)
            {
                this.ConnectingString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath(FolderPath + "/" + FileName) + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
            }
            else if (ReadHeader == enmFileReadHeader.NotAllow)
            {
                this.ConnectingString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath(FolderPath + "/" + FileName) + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
            }
            this.Connect();
        }

        public clsConnection(string ServerName, string DatabaseName, string UserName, string Password)
        {
            this.DBType = enmDBType.MS_SQL;
            this.ConnectingString = "server=" + ServerName + ";uid=" + UserName + ";pwd=" + Password + ";database=" + DatabaseName;
            this.Connect();
        }

        private void _Connect_MS_SQL()
        {
            if (Convert.ToBoolean(~Convert.ToInt32(ConnectionState.Open)))
            {
                try
                {
                    _DB_MS_SQL = new SqlConnection(this._ConnectingString);
                    _DB_MS_SQL.Open();
                }
                catch (Exception ex)
                {
                    clsFile.WriteLog("clsConnection", "_Connect_MS_SQL", ex.ToString());
                }
            }
        }

        private void _Disconnect_MS_SQL()
        {
            if (!Convert.ToBoolean(~Convert.ToInt32(ConnectionState.Closed)))
            {
                try
                {
                    this._DB_MS_SQL.Close();
                    this._DB_MS_SQL.Dispose();
                    this._DB_MS_SQL = null;
                }
                catch (Exception ex)
                {
                    clsFile.WriteLog("clsConnection", "_Disconnect_MS_SQL", ex.ToString());
                }
            }
        }

        private void _Init()
        {
            this._DBType = enmDBType.NotDefined;
            this._DB_MS_SQL = null;
            this._ConnectingString = "";
        }

        protected virtual void Connect()
        {
            switch (this.DBType)
            {
                case enmDBType.MS_SQL:
                    this._Connect_MS_SQL();
                    return;
            }
        }

        protected virtual void Disconnect()
        {
            switch (this.DBType)
            {
                  case enmDBType.MS_SQL:
                    this._Disconnect_MS_SQL();
                    return;
            }
        }

        public virtual void Dispose()
        {
            this.Disconnect();
        }

        ~clsConnection()
        {
            this.Disconnect();
        }

        public string ConnectingString
        {
            get
            {
                return this._ConnectingString;
            }
            set
            {
                if(value != null)
                {
                    if (value.Trim() != "")
                    {
                        this._ConnectingString = value;
                    }
                }
            }
        }

        public object Connection
        {
            get
            {
                switch (this.DBType)
                {
                     case enmDBType.MS_SQL:
                     return this._DB_MS_SQL;
                }
                return null;
            }
            set
            {
                switch (this.DBType)
                {
                    case enmDBType.MS_SQL:
                    this._DB_MS_SQL = (SqlConnection) value;
                    return;
                }
            }
        }

        public enmDBType DBType
        {
            get
            {
                return this._DBType;
            }
            set
            {
                if ((value > enmDBType.NotDefined) && (value < enmDBType.MaxValue))
                {
                    this._DBType = value;
                }
            }
        }

        public string Status
        {
            get
            {
                switch (this._DBType)
                {
                    case enmDBType.MS_SQL:
                    return this._DB_MS_SQL.State.ToString();
                }
                return "";
            }
        }

        private string _ConnectingString;
        private SqlConnection _DB_MS_SQL;
        private enmDBType _DBType;
    }
}

